//
//  XS_ZhengshuDetailVC.swift
//  XueDao
//
//  Created by 韩云智 on 2020/12/14.
//

import UIKit
import RxSwift
import RxCocoa

class XS_recommendCurriculumIpageRecordsM: XS_BaseModel {
    var id = 0 // 推荐课程id
    var curriculumName = "" // 推荐课程名称
    var introduce = "" // 推荐课程简介
    var curriculumImgUrl = "" // 推荐课程图片
    var curriculumForm = "online"
}
class XS_recommendCurriculumIpageM: XS_BaseModel {
    var records: [XS_recommendCurriculumIpageRecordsM] = [] // 推荐课程集合
    var total = 0 // 总记录数
    var size = 0 // 一页的记录数
    var current = 0 // 当前页数
    var searchCount = false
    var pages = 1 // 总页数
}
class XS_certificateM: XS_BaseModel {
    var id = 0 // 证书id
    var industryId = 0 // 行业编号
    var name = "" // 证书名称
    var level = "" // 证书等级
    var imgUrl = "" // 证书图片地址
    var count = 0 // 参考人数
    var unit = "" // 发证单位
    var advantage = "" // 优势
    var testPlan = "" // 考试计划
    var testWay = "" // 考试方式
    var marketPrice = "" // 市场价格
    var registrationProcess = "" // 报名流程
    var description = "" // 证书描述
    var industryName = "" // 行业名称
    var detailsImgUrl = "" // 详情页证书图片
}
class XS_queryByIdM: XS_BaseModel {
    var recommendCurriculumIpage = XS_recommendCurriculumIpageM()
    var certificate = XS_certificateM()
}

class XS_ZhengshuDetailVC: XS_BaseVC {
    var id: Int!
    lazy var changeModel = XS_recommendCurriculumIpageM()
    
    override func upConfig() {
        isStatusBarBlack = false
        shouldNavBarHidden = true
    }
    override func upView() {
        title = "证书详情"
        
        let imgV = UIImageView(image: UIImage(named: "首页-证书背景"))
        view.addSubview(imgV)
        imgV.snp.makeConstraints({
            $0.left.top.right.equalToSuperview()
            $0.height.equalTo(.scale*190)
        })
        loadCustomBar()
        
        let scroll = UIScrollView()
        scroll.bounces = false
        scroll.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        scroll.clipsToBounds = true
        scroll.contentInsetAdjustmentBehavior = .never
        view.addSubview(scroll)
        scroll.snp.makeConstraints({
            $0.top.equalTo(customBar.snp.bottom)
            $0.left.right.bottom.equalToSuperview()
        })
        let top = UIImageView(image: UIImage(named: "首页-证书背景"))
        scroll.addSubview(top)
        top.snp.makeConstraints({
            $0.left.width.equalToSuperview()
            $0.height.equalTo(.scale*190)
            $0.top.equalToSuperview().offset(-CGFloat.barHeight - 44)
        })
        
        let cImgV = UIImageView()
        cImgV.layer.masksToBounds = true
        cImgV.layer.cornerRadius = .scale*4
        cImgV.layer.shadowColor = UIColor.red.cgColor;//阴影颜色
        cImgV.layer.shadowOffset = CGSize(width: 5,height: 5);//偏移距离
        cImgV.layer.shadowOpacity = 0.8;//不透明度
        cImgV.layer.shadowRadius = 5;//半径
        
        scroll.addSubview(cImgV)
        cImgV.snp.makeConstraints {
            $0.top.equalTo(.scale*22)
            $0.centerX.equalToSuperview()
            $0.width.equalTo(.scale*285)
            $0.height.equalTo(.scale*200)
        }
        let tLab = UILabel.xsLabel(size: .scale*18, fontName: .semibold, color: #colorLiteral(red: 0.2196078431, green: 0.2352941176, blue: 0.2470588235, alpha: 1), alignment: .center)
        scroll.addSubview(tLab)
        tLab.snp.makeConstraints({
            $0.centerX.equalToSuperview()
            $0.top.equalTo(cImgV.snp.bottom).offset(.scale*22)
        })
        
        let backView = UIView()
        backView.layer.corner(radius: .scale*4, bgColor: #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0), isShadow: true, sColor: #colorLiteral(red: 0.1843137255, green: 0.2039215686, blue: 0.2039215686, alpha: 1), offset: CGSize(width: 0, height: .scale*2), effect: .scale*12, opacity: 0.06)
        scroll.addSubview(backView)
        backView.snp.makeConstraints({
            $0.width.equalTo(.scale*343)
            $0.centerX.equalToSuperview()
            $0.top.equalTo(tLab.snp.bottom).offset(.scale*17)
        })
        
        let dLab = UILabel.xsLabel(size: .scale*12, fontName: .regular, color: #colorLiteral(red: 0.6666666667, green: 0.6941176471, blue: 0.7176470588, alpha: 1), lines: 0)
        scroll.addSubview(dLab)
        dLab.snp.makeConstraints({
            $0.left.equalTo(.scale*16)
            $0.right.equalTo(-.scale*16)
            $0.width.equalTo(view.frame.size.width - 2 * .scale*16)
            $0.top.equalTo(backView.snp.bottom).offset(.scale*16)
        })
        
        let line = UIView()
        line.backgroundColor = #colorLiteral(red: 0.968627451, green: 0.968627451, blue: 0.968627451, alpha: 1)
        scroll.addSubview(line)
        line.snp.makeConstraints({
            $0.height.equalTo(.scale*8)
            $0.top.equalTo(dLab.snp.bottom).offset(.scale*16)
            $0.left.width.equalToSuperview()
        })
        
        
        let collections = XD_RecommendCollView()
        scroll.addSubview(collections)
        collections.snp.makeConstraints({
            $0.top.equalTo(line.snp.bottom)
            $0.height.equalTo(.scale*160)
            $0.bottom.lessThanOrEqualToSuperview().offset(-CGFloat.bottomHeight)
            $0.left.width.equalToSuperview()
        })
        
        XS_NetWork.requestJson("/certificate/queryById", parameters: ["id":id!])
            .mapModel(type: XS_queryByIdM.self)
            .subscribe(onNext: { [weak self] model, resp in
                guard let `self` = self else { return }
                guard resp.success, let model = model else {
                    xsWindow()?.xsShowMsg("证书获取失败!")
                    self.navigationController?.popViewController(animated: true)
                    return
                }
                cImgV.sd_setImage(with: URL(string: model.certificate.detailsImgUrl))
                tLab.text = model.certificate.name
                
                let tempTitlesArr = ["发证单位","级别","考试计划","考试方式","市场价格","报名流程"]
                var titlesArr: [String] = []
                var detialsArr: [String] = []
                
                for (index, text) in [
                    model.certificate.unit,
                    model.certificate.level,
//                    model.certificate.advantage,
                    model.certificate.testPlan,
                    model.certificate.testWay,
                    model.certificate.marketPrice,
                    model.certificate.registrationProcess
                ].enumerated() {
                    if text.count > 0 {
                        detialsArr.append(text)
                        titlesArr.append(tempTitlesArr[index])
                    }
                }
                
                self.getItemViews(superView: backView, titlesArr: titlesArr, detailsArr: detialsArr)
                
                dLab.text = model.certificate.description
                collections.certificateId = model.certificate.id
                collections.reLoadData()
            }).disposed(by: disposeBag)
    }
    
    private func getItemViews(superView: UIView, titlesArr: [String], detailsArr: [String]) {
        
        var lastView: UIView?
        
        for (index, text) in detailsArr.enumerated() {
            let textView = UIView()
            superView.addSubview(textView)
            textView.snp.makeConstraints({
                if let lastV = lastView {
                    $0.top.equalTo(lastV.snp.bottom)
                } else {
                    $0.top.equalToSuperview()
                }
                if index == detailsArr.count - 1 {
                    $0.bottom.equalToSuperview()
                }
                $0.left.equalTo(.scale * 13)
                $0.right.equalTo(-.scale * 13)
            })
            
            let titlesLab = UILabel.xsLabel(size: .scale*14, fontName: .regular, color: #colorLiteral(red: 0.2196078431, green: 0.2352941176, blue: 0.2470588235, alpha: 1), text: titlesArr[index])
            textView.addSubview(titlesLab)
            titlesLab.snp.makeConstraints({
                $0.top.equalTo(.scale * 14)
                $0.left.equalTo(.scale * 27)
                $0.width.equalTo(.scale * 76)
                $0.height.equalTo(.scale * 20)
            })
            
            let detailsLab = UILabel.xsLabel(size: .scale*14, fontName: .regular, color: #colorLiteral(red: 0.4470588235, green: 0.4666666667, blue: 0.4862745098, alpha: 1), text: text)
            detailsLab.numberOfLines = 0
            textView.addSubview(detailsLab)
            detailsLab.snp.makeConstraints({
                $0.top.equalTo(titlesLab)
                $0.left.equalTo(titlesLab.snp.right)
                $0.right.equalTo(-.scale * 27)
            })
            
            let lineV = UIView()
            lineV.isHidden = false
            lineV.backgroundColor =  UIColor(red: 0.90, green: 0.91, blue: 0.95, alpha: 1.0)
            textView.addSubview(lineV)
            lineV.snp.makeConstraints { (make) in
                make.top.equalTo(detailsLab.snp.bottom).offset(.scale * 13)
                make.left.right.equalToSuperview()
                make.height.equalTo(.scale * 0.5)
                make.bottom.equalToSuperview()
            }
            
            if index == detailsArr.count - 1 {
                lineV.isHidden = true
            }
            
            lastView = textView
        }
    }
}
